//
//  MiMaDenghLuViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/1.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "MiMaDenghLuViewController.h"
#import "GXtabbarController.h"
#import "AppDelegate.h"
@interface MiMaDenghLuViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingScrollView *mainScrollView;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *allViewHeight;
@property (weak, nonatomic) IBOutlet UITextField *passwordTf;
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *textFieldArray;
@end

@implementation MiMaDenghLuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.customNavBar.title = @"";
    
    [self.customNavBar wr_setBackgroundAlpha:0];
    
   
    if (@available(iOS 11.0, *)) {
        self.mainScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    _allViewHeight.constant = ScreenHeight;
    
    _topSpace.constant = StatusBarHeight+80;
    
    CAGradientLayer *gl1 = [CAGradientLayer layer];
    gl1.frame = CGRectMake(0,0,ScreenWidth,ScreenHeight);
    gl1.startPoint = CGPointMake(1, 0.3);
    gl1.endPoint = CGPointMake(1, 0.1);
    gl1.colors = @[(__bridge id)rgba(255, 255, 255, 1).CGColor, (__bridge id)rgba(203, 222, 255, 1).CGColor];
    gl1.locations = @[@(0), @(1.0f)];
   
    [_backView.layer insertSublayer:gl1 atIndex:0];
    [_phoneTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
}
-(void)textFieldDidChange:(UITextField *)textField
{
    CGFloat maxLength = 11;
    NSString *toBeString = textField.text;
    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position || !selectedRange)
    {
        if (toBeString.length > maxLength)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:maxLength];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}
//登录
- (IBAction)dengLu:(UIButton *)sender {
    
    [self.view endEditing:YES];
    for (UITextField *tf in _textFieldArray) {
        if(tf.text.length == 0){
            [ZZPUtils showWarning:tf.placeholder];
            return;
        }
    }
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:_phoneTf.text forKey:@"username"];
    [paraDic setValue:_passwordTf.text forKey:@"password"];
    [paraDic setValue:@"1" forKey:@"loginType"];
    [paraDic setValue:@"2" forKey:@"type"];
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/login" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            NSDictionary *data = [info objectForKey:@"data"];
            [ZZPUtils saveUserDefultsData:GetHandleString(data, @"adminToken") forKey:@"adminToken"];
            
            [self baoCunToken];
            
            [self getInfoFromServer];
        }
    }];
}
//新增或更新用户标识
- (void)baoCunToken{
    if([[ZZPUtils readUserDefultsDataForKey:@"deviceTokenString"] length] > 0){
        NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
        [paraDic setValue:@"2" forKey:@"deviceType"];
        [paraDic setValue:[ZZPUtils readUserDefultsDataForKey:@"deviceTokenString"] forKey:@"deviceToken"];
        [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/adminUmeng/deviceToken" andParas:paraDic andShowHud:NO andTransferGottenData:^(id info, NSError *error) {
            if (!error) {
                NSLog(@"info:%@",info);
            }
        }];
    }
}


- (void)getInfoFromServer{
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/adminUser/queryLoginUser" andParas:nil andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            NSLog(@"%@",info);
            
            [ZZPUtils saveUserDefultsData:[ZZPUtils deleteEmpty:[info objectForKey:@"data"]] forKey:@"userInfo"];
            
            GXtabbarController *tabBarCtn=[[GXtabbarController alloc]init];
            [AppDelegate shareInstance].tabBarController = tabBarCtn;
            [AppDelegate shareInstance].window.rootViewController = tabBarCtn;
        }
    }];
}


//密码登录
- (IBAction)miMa:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)show:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    _passwordTf.secureTextEntry = !sender.selected;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


